package net.sk.china.city.talk.service.impl.system;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import net.sk.china.city.talk.model.system.CityAdmin;
import net.sk.china.city.talk.model.system.CityMenu;
import net.sk.china.city.talk.response.system.LoginSystemResponse;
import net.sk.china.city.talk.response.system.SystemPermissionsResponse;
import net.sk.china.city.talk.service.system.SystemAdminService;
import net.sk.china.city.talk.service.system.SystemMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 系统管理员账户验证处理
 * +----------------------------------------------------------------------
 * | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2018-2024 聊来All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * +----------------------------------------------------------------------
 * | @Author: 聊来 <18970881148@qq.com>
 * +----------------------------------------------------------------------
 * | DateTime: 2024/6/2 15:23
 * +----------------------------------------------------------------------
 */

@Service
public class AdminDetailServiceImpl implements UserDetailsService {

    private static final Logger log = LoggerFactory.getLogger(AdminDetailServiceImpl.class);

    private SystemAdminService systemAdminService;
    private SystemMenuService systemMenuService;

    @Autowired
    public void setSystemAdminService(SystemAdminService systemAdminService) {
        this.systemAdminService = systemAdminService;
    }

    @Autowired
    public void setSystemMenuService(SystemMenuService systemMenuService) {
        this.systemMenuService = systemMenuService;
    }


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        CityAdmin admin = systemAdminService.selectUserByUserName(username);
        if (ObjectUtil.isNull(admin)) {
            log.info("登录用户：{} 不存在.", username);
            throw new UsernameNotFoundException("登录用户：" + username + " 不存在");
        } else if (!admin.getStatus()) {
            log.info("登录用户：{} 已被停用.", username);
            throw new UsernameNotFoundException("对不起，您的账号：" + username + " 已停用");
        }
        return createLoginAdmin(admin);
    }

    private UserDetails createLoginAdmin(CityAdmin admin) {
        List<Integer> roles = Stream.of(admin.getRoles().split(",")).map(Integer::valueOf).collect(Collectors.toList());
        List<CityMenu> menuList;
        // 超级管理员 获取全部权限
        if (roles.contains(1)) {
            menuList = systemMenuService.getAllPermissions();
        } else {
            menuList = systemMenuService.findPermissionByUserId(admin.getId());
        }
        menuList = menuList.stream().filter(e -> StrUtil.isNotEmpty(e.getPerms())).collect(Collectors.toList());

        List<SystemPermissionsResponse> permissionsList = menuList.stream().map(menu -> {
            SystemPermissionsResponse permissions = new SystemPermissionsResponse();
            permissions.setId(menu.getId());
            permissions.setPid(menu.getPid());
            permissions.setName(menu.getName());
            permissions.setPath(menu.getPerms());
            permissions.setSort(menu.getSort());
            return permissions;
        }).collect(Collectors.toList());

        return new LoginSystemResponse(admin, permissionsList);
    }
}
